﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Api.ContentProvider;
using Api.Model;
using ThemeLoader;

namespace Core.ScreenStore.View
{
    [TemplatePart(Name = "Content",Type=typeof(StackPanel))]

    [TemplateVisualState(Name = "Normal", GroupName = "CommonStates")]
    [TemplateVisualState(Name = "MouseOver", GroupName = "CommonStates")]
    [TemplateVisualState(Name = "Pressed", GroupName = "CommonStates")]
    public partial class ArticleDetailsView : Control
    {
        public FrameworkElement LayoutRoot = null;
        public StackPanel Content;

        public Article Article
        {
            get { return (Article)GetValue(ArticleProperty); }
            set { SetValue(ArticleProperty, value); }
        }

        public static readonly DependencyProperty ArticleProperty =
            DependencyProperty.Register("Article", typeof(Article), typeof(ArticleDetailsView), new PropertyMetadata(ArticlePropertyChanged));

        private static void ArticlePropertyChanged(DependencyObject articleDetailsView, DependencyPropertyChangedEventArgs e)
        {
            ((ArticleDetailsView)articleDetailsView).FillArticleList();
        }
    
        public IContentProvider ContentProvider
        {
            get { return (IContentProvider)GetValue(ContentProviderProperty); }
            set { SetValue(ContentProviderProperty, value); }
        }

        public static readonly DependencyProperty ContentProviderProperty =
            DependencyProperty.Register("ContentProvider", typeof(IContentProvider), typeof(ArticleDetailsView), new PropertyMetadata(ProviderPropertyChanged));

        private static void ProviderPropertyChanged(DependencyObject articleDetailsView, DependencyPropertyChangedEventArgs e)
        {
            ((ArticleDetailsView)articleDetailsView).FillArticleList();
        }

        public IResourceProvider ResourceProvider
        {
            get { return (IResourceProvider)GetValue(ResourceProviderProperty); }
            set { SetValue(ResourceProviderProperty, value); }
        }

        public static readonly DependencyProperty ResourceProviderProperty =
            DependencyProperty.Register("ResourceProvider", typeof(IResourceProvider), typeof(ArticleDetailsView), null);

        
        public ArticleDetailsView()
        {
            DefaultStyleKey = typeof(ArticleDetailsView);
        }
    
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            Content = GetTemplateChild("Content") as StackPanel;
            FillArticleList();
            RegisterToChildEvents();
        }

        public void FillArticleList()
        {
            if (Article == null) return;
            if (Content == null) return;
            if (Article.Items == null) return;

            Content.Children.Clear();

            foreach (Item item in Article.Items)
            {
                var mapping = ContentProvider.GetItemsMapping(item.Type);
                var control = mapping(item.Value);

                if (ResourceProvider != null && control is FrameworkElement)
                {
                    var style = (Style) ResourceProvider.GetResource(item.Type + "ArticleStyle");
                    (control as FrameworkElement).Style = style;
                }

                Content.Children.Add(control);
            }
        }
        private void RegisterToChildEvents()
        {
            if (Content == null) return;
            Content.MouseEnter += new MouseEventHandler(Content_MouseEnter);
        }

        void Content_MouseEnter(object sender, MouseEventArgs e)
        {
            VisualStateManager.GoToState(this, "MouseOver", true);
        }
    }
}
